from django.contrib import admin
from django.utils.html import format_html
from mysqlModel.models import *
from django.contrib.auth.decorators import permission_required as django_permission_required
from django.http import HttpResponseForbidden


# 必须继承ModelAdmin基类,才可以调整参数,HostDB则是你的表的名称
@admin.register(data)
class MyAdmin(admin.ModelAdmin):
    admin.site.site_title = "云智能平台精简安装包"
    admin.site.site_header = "云智能平台精简安装包"
    list_display_links = None
    # change_list_template = 'index.html'
    # list_display = 你需要或者想要展示在页面中的字段
    list_display = (
        "Name", 'Soft_version', 'Cpu', 'License', 'Email', 'Create_time', 'Updatetime', 'Status', 'Operate')
    # search_fields = 添加搜索功能,并以HostName作为搜索条件
    search_fields = ("name",)
    list_per_page = 10

    def render_change_form(self, request, context, add=False, change=False, form_url='', obj=None):
        context.update({
            'show_save': True,
            'show_save_and_continue': False,
            'show_save_and_add_another': False,
            'delete': False,
        })
        return super().render_change_form(request, context, add, change, form_url, obj)

    def has_delete_permission(self, request, obj=None):
        # 禁用删除按钮
        return False

    # 权限功能
    def has__delete__permission(self, request):
        user_per_set = request.user.get_all_permissions()  # 获取当前用户权限
        # 待判断的权限范围
        print(user_per_set, 'user_per_set')
        curr_per_set = {'myapp.view_job_detail', 'myapp.delete_job_detail', 'myapp.add_job_detail',
                        'myapp.change_job_detail'}
        print(curr_per_set.intersection(user_per_set), 'curr_per_set.intersection(user_per_set)')
        if 'mysqlModel.delete_data' in curr_per_set.intersection(user_per_set):
            return True
        else:
            return False



    save_as_continue = False  # 修改完成之后跳转到元素列表页面

    @admin.display(description='状态')
    def Status(self, obj):
        colours = ['#00ff009c', '#b23375', '#F5ECCF', '#F1E7A9', '#FF6770']
        return format_html('''
        <span style="padding: 2px 8px;
        font-size: 0.7rem;
        background-color:{colour};
        letter-spacing: 1px;
        color: white;
        border-radius: 2px;">{status}</span>
        ''', colour=colours[int(obj.stats)], status=data._meta.get_field('stats').choices[int(obj.stats)][1])

    @admin.display(description='客户名称')
    def Name(self, obj):
        return obj.name

    @admin.display(description='软件版本')
    def Soft_version(self, obj):

        return obj.soft_version

    @admin.display(description='CPU型号')
    def Cpu(self, obj):
        return obj.cpu

    @admin.display(description='License文件')
    def License(self, obj):
        return str(obj.license).split('/')[-1]
        # return obj.license.name

    # @receiver(pre_delete, sender=data)
    # def handle_camera_deleted(instance: data, **_):
    #     instance.license.delete(save=False)

    @admin.display(description='发送邮箱')
    def Email(self, obj):
        return obj.email

    @admin.display(description='更新时间')
    def Updatetime(self, obj):
        return obj.update_time

    @admin.display(description='创建时间')
    def Create_time(self, obj):
        return obj.create_time

    @admin.display(description='操作')
    def Operate(self, obj):
        if obj.stats == '0':
            return format_html(r'''
            <a style="padding:2px;cursor:pointer;color:#409eff" onclick="deleteItem({rid})">删除</a>
            ''', rid=obj.id)
            # return format_html('')
        elif obj.stats == '1':
            return format_html('''
            <a style="padding:2px;cursor:pointer;color:#409eff" onclick="changestats({rid})">暂停生成</a>
            ''', rid=obj.id)
        elif obj.stats == '2':
            return format_html('''
            <a style="padding:2px;cursor:pointer;color:#409eff" onclick="checkpro({rid})">查看进度</a>
            ''', rid=obj.id)
        elif obj.stats == '3':
            return format_html('''
            <a style="padding:2px;cursor:pointer;color:#409eff" onclick="deleteItem({rid})">删除</a>
            ''', rid=obj.id)
        elif obj.stats == '4':
            # return format_html('''
            # <a style="padding:2px;cursor:pointer;color:#409eff" onclick="deleteItem({rid})">删除</a>
            # <a style="padding:2px;cursor:pointer;color:#409eff" onclick="changestats({rid})">继续生成</a>
            # ''', rid=obj.id)
            return format_html('''
            <a style="padding:2px;cursor:pointer;color:#409eff" onclick="changestats({rid})">继续生成</a>
            ''', rid=obj.id)

    class Media:
        js = ('js/license-func.js',)
